// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


/*
 * Created by: Stefan Kyntchev
 * Created: Monday, February 26, 2007
 */

using System.Collections.Generic;
using Microsoft.WindowsMobile.Status;

namespace svs.Mobile.BeyondMedia.FeedCore.RSS.Templates
{
	public class FeedItemTemplate : TemplateBase
	{
		private static List<KeyValuePair<string, string>> m_Template;
		private bool m_Downloaded;
		private bool m_InHistory;
		private static readonly string m_OwnerEmail = SystemState.OwnerEmail;




		public override List<KeyValuePair<string, string>> Template
		{
			get
			{
				if (m_Template == null)
				{
					m_TemplateFileName = "RSSFeedItem.htm";
					m_Template = LoadTemplate();
				}
				return m_Template;
			}
		}

		public FeedItemTemplate(TemplateTypes type) : base(type)
		{
			OwnerEmail = m_OwnerEmail;
		}

		/// <summary>
		/// The name of the feed
		/// </summary>
		public string Title
		{
			set
			{
				m_Parameters["#FEEDITEM_TITLE#"] = value;
				m_Parameters["#FEEDITEM_TITLE_URLENCODED#"] = value.Replace("&", " ").Replace("\""," ");
			}
			get { return m_Parameters["#FEEDITEM_TITLE#"]; }
		}

		/// <summary>
		/// The Description of the feed
		/// </summary>
		public string Description
		{
			set { m_Parameters["#FEEDITEM_DESCRIPTION#"] = value; }
			get { return m_Parameters["#FEEDITEM_DESCRIPTION#"]; }
		}

		/// <summary>
		/// The Link to the feed URL
		/// </summary>
		public string Link
		{
			set { m_Parameters["#FEEDITEM_LINK#"] = value; }
			get { return m_Parameters["#FEEDITEM_LINK#"]; }
		}

		/// <summary>
		/// Unique ID that identifies this item across all feeds
		/// </summary>
		public string FeedItemID
		{
			set
			{
				m_Parameters["#FEED_ITEM_ID#"] = value;
			}
			get
			{
				return m_Parameters["#FEED_ITEM_ID#"];
			}
		}

		/// <summary>
		/// The Link to the feed URL
		/// </summary>
		public string EnclosureLink
		{
			set { m_Parameters["#FEEDITEM_ENCLOSURELINK#"] = value; }
			get { return m_Parameters["#FEEDITEM_ENCLOSURELINK#"]; }
		}

		public string EnclosureStreamLink
		{
			set
			{
				m_Parameters["#FEEDITEM_ENCLOSURE_STREAM_LINK#"] = value;
			}
			get
			{
				return m_Parameters["#FEEDITEM_ENCLOSURE_STREAM_LINK#"];
			}
		}

		public string EnclosureFileName
		{
			set { m_Parameters["#FEEDITEM_ENCLOSURE_FILE_NAME#"] = value; }
			get { return m_Parameters["#FEEDITEM_ENCLOSURE_FILE_NAME#"]; }
		}

		public string ImageFolder
		{
			set
			{
				m_Parameters["#IMAGE_FOLDER#"] = value;
			}
			get
			{
				return m_Parameters["#IMAGE_FOLDER#"];
			}
		}

		public string LinkPrefix
		{
			set
			{
				m_Parameters["#FEEDITEM_LINK_PREFIX#"] = value;
			}
			get
			{
				return m_Parameters["#FEEDITEM_LINK_PREFIX#"];
			}
		}

		public string EnclosureFileSize
		{
			set { m_Parameters["#FEEDITEM_ENCLOSURE_FILE_SIZE#"] = value; }
			get { return m_Parameters["#FEEDITEM_ENCLOSURE_FILE_SIZE#"]; }
		}

		public string EnclosureAction
		{
			set
			{
				if (!EnclosureIsDownloaded && !EnclosureInHistory)
				{
					m_Parameters["#FEEDITEM_ENCLOSURE_ACTION#"] = string.IsNullOrEmpty(value)
					                                              	? string.Empty
					                                              	: string.Format("<img src='{0}/download.png'/>&nbsp;{1}",Configuration.RssTemplatesImagePath,value);
				}
				else if (!EnclosureIsDownloaded && EnclosureInHistory)
					m_Parameters["#FEEDITEM_ENCLOSURE_ACTION#"] = string.IsNullOrEmpty(value)
					                                              	? string.Empty
																													: string.Format("<img src='{0}/download.png'/>&nbsp;{1}",Configuration.RssTemplatesImagePath, value);
				else
					m_Parameters["#FEEDITEM_ENCLOSURE_ACTION#"] = string.IsNullOrEmpty(value)
					                                              	? string.Empty
					                                              	: string.Format("<img src='{0}/play.png'/>&nbsp;{1}",Configuration.RssTemplatesImagePath, value);
			}
			get { return m_Parameters["#FEEDITEM_ENCLOSURE_ACTION#"]; }
		}


		public string EnclosureStreamAction
		{
			set
			{
				if (!EnclosureIsDownloaded)
				{
					m_Parameters["#FEEDITEM_ENCLOSURE_STREAM_ACTION#"] = string.IsNullOrEmpty(value)
																													? string.Empty
																													: string.Format("<img src='{0}/play.png'/>&nbsp;{1}", Configuration.RssTemplatesImagePath, value);
				}
				else
					m_Parameters["#FEEDITEM_ENCLOSURE_STREAM_ACTION#"] = string.Empty;
			}
			get
			{
				return m_Parameters["#FEEDITEM_ENCLOSURE_STREAM_ACTION#"];
			}
		}

		public string EnclosureDeleteAction
		{
			set
			{
				if (EnclosureIsDownloaded)
				{
					m_Parameters["#FEEDITEM_ENCLOSURE_DELETE_ACTION#"] = string.IsNullOrEmpty(value)
																													? string.Empty
																													: string.Format("<img src='{0}/delete.png'/>&nbsp;{1}", Configuration.RssTemplatesImagePath, value);
				}
				else
					m_Parameters["#FEEDITEM_ENCLOSURE_DELETE_ACTION#"] = string.Empty;
			}
			get
			{
				return m_Parameters["#FEEDITEM_ENCLOSURE_DELETE_ACTION#"];
			}
		}

		public bool EnclosureIsDownloaded
		{
			set
			{
				m_Downloaded = value;
				m_Parameters["#FEEDITEM_ENCLOSURE_IS_DOWNLOADED_IND#"] = string.Empty;
			}
			get { return m_Downloaded; }
		}

		public bool EnclosureInHistory
		{
			set { m_InHistory = value; }
			get { return m_InHistory; }
		}

		// <summary>
		/// The Description of the feed
		/// </summary>
		public string PublishedDate
		{
			set { m_Parameters["#FEEDITEM_PUBDATE#"] = value; }
			get { return m_Parameters["#FEEDITEM_PUBDATE#"]; }
		}

		/// <summary>
		/// Unique sequence id of the item - unique per feed - first item in the feed starts withn sequence 1, second 2 etc.
		/// </summary>
		public string FeedItemSequenceID
		{
			set
			{
				m_Parameters["#FEED_ITEM_SEQUENCE_ID#"] = value;
			}
			get
			{
				return m_Parameters["#FEED_ITEM_SEQUENCE_ID#"];
			}
		}

		/// <summary>
		/// Unique id of the item (for HTML element id). This is unque per page"
		/// </summary>
		public string FeedPageItemID
		{
			set { m_Parameters["#FEED_PAGE_ITEM_ID#"] = value; }
			get { return m_Parameters["#FEED_PAGE_ITEM_ID#"]; }
		}

		/// <summary>
		/// The display style of the DIV for the Item description
		/// </summary>
		public string DescriptionVisibility
		{
			set { m_Parameters["#DESCRIPTION_VISIBILITY#"] = value; }
			get { return m_Parameters["#DESCRIPTION_VISIBILITY#"]; }
		}

		/// <summary>
		/// The image used for visibility toggle
		/// </summary>
		public string DescriptionVisibilityToggleImage
		{
			set { m_Parameters["#TOGGLE_IMG#"] = value; }
			get { return m_Parameters["#TOGGLE_IMG#"]; }
		}

		/// <summary>
		/// The visibility of the toggle section
		/// </summary>
		public string ToggleVisibility
		{
			set { m_Parameters["#TOGGLE_VISIBILITY#"] = value; }
			get { return m_Parameters["#TOGGLE_VISIBILITY#"]; }
		}

		/// <summary>
		/// The visibility of the enclosure
		/// </summary>
		public string EnclosureVisibility
		{
			set { m_Parameters["#FEEDITEM_ENCLOSURE_VISIBILITY#"] = value; }
			get { return m_Parameters["#FEEDITEM_ENCLOSURE_VISIBILITY#"]; }
		}

		/// <summary>
		/// The text for More/Less
		/// </summary>
		public string ToggleText
		{
			set { m_Parameters["#TOGGLE_TEXT#"] = value; }
			get { return m_Parameters["#TOGGLE_TEXT#"]; }
		}

		public string ReadText
		{
			set
			{
				m_Parameters["#READ_TEXT#"] = value;
			}
			get
			{
				return m_Parameters["#READ_TEXT#"];
			}
		}

		public string ReadStatusColor
		{
			set
			{
				m_Parameters["#ITEM_READ_STATUS_COLOR#"] = value;
			}
			get
			{
				return m_Parameters["#ITEM_READ_STATUS_COLOR#"];
			}
		}

		/// <summary>
		/// The Owner e-mail as stored in the device 
		/// </summary>
		public string OwnerEmail
		{
			set
			{
				m_Parameters["#OWNER_EMAIL#"] = value;
			}
			get
			{
				return m_Parameters["#OWNER_EMAIL#"];
			}
		}

		public static void RefreshTemplate()
		{
			m_Template = null;
		}
	}
}